//package cn.xie.blog.servergateway.filler;
//
//import jakarta.annotation.Resource;
//import jakarta.servlet.FilterChain;
//import jakarta.servlet.ServletException;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.BadCredentialsException;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
//
//import java.io.IOException;
//
///**
// * @author: xiaoxie
// * create: 2025-05-01 17:16
// * @BelongsProject: blog-parent
// * @BelongsPackage: cn.xie.blog.servergateway.filler
// * description: 自定义的登录处理
// */
//public class CustomUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
//
//
//    private final AuthenticationManager authenticationManager;
//
//    public CustomUsernamePasswordAuthenticationFilter(AuthenticationManager authenticationManager) {
//        this.authenticationManager = authenticationManager;
//        setAuthenticationManager(authenticationManager); // 👈这行最关键！
//    }
//
//    @Override
//    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
//        try {
//            // 获取请求体中的用户名和密码
//            String username = request.getParameter("username");
//            String password = request.getParameter("password");
//
//            // 创建认证令牌
//            UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
//
//            // 设置详细信息
//            setDetails(request, authRequest);
//
//            // 进行认证
//            return authenticationManager.authenticate(authRequest);
//        } catch (Exception e) {
//            throw new BadCredentialsException("Invalid username or password");
//        }
//    }
//
//    @Override
//    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
//        // 登录成功后的处理逻辑
//        try {
//            super.successfulAuthentication(request, response, chain, authResult);
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        } catch (ServletException e) {
//            throw new RuntimeException(e);
//        }
//    }
//
//    @Override
//    protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException {
//        // 登录失败后的处理逻辑
//        super.unsuccessfulAuthentication(request, response, failed);
//    }
//}
